package com.example.medical_project.modules.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.medical_project.commons.utils.UserUtils;
import com.example.medical_project.modules.system.domain.DictData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical_project.modules.system.domain.User;
import com.example.medical_project.modules.system.dto.DictDataDto;
import com.example.medical_project.modules.system.service.DictDataService;
import com.example.medical_project.modules.system.mapper.DictDataMapper;


import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 86157
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2025-01-08 16:42:27
*/
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData>
    implements DictDataService{

    @Override
    public Page<DictData> listForPage(DictDataDto params) {

        Page<DictData> page = new Page<>(params.getPageNum(),params.getPageSize());

        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(StrUtil.isNotBlank(params.getDictType()),DictData::getDictType,params.getDictType());
        queryWrapper.like(StrUtil.isNotBlank(params.getDictLabel()),DictData::getDictLabel,params.getDictLabel());
        queryWrapper.eq(StrUtil.isNotBlank(params.getStatus()),DictData::getStatus,params.getStatus());

        Page<DictData> result = baseMapper.selectPage(page,queryWrapper);

        return result;


    }

    @Override
    public void addDictData(DictDataDto params) {

        DictData dictData = new DictData();

//        将用户界面的数据传到dictData中
        BeanUtil.copyProperties(params,dictData);

        //设置创建时间
        dictData.setCreateTime(new Date());

        //获取当前登录用户
        User user = UserUtils.getLoginUser();

        //设置创建者
        dictData.setCreateBy(user.getUserName());

        this.save(dictData);


    }

    @Override
    public void updateDictData(DictDataDto params) {

        DictData dictData = new DictData();

        BeanUtil.copyProperties(params,dictData);

        dictData.setCreateTime(new Date());

        User user = UserUtils.getLoginUser();

        dictData.setCreateBy(user.getUserName());

        this.updateById(dictData);

    }


}




